/**
 * 优先队列
 * 通过优先级加入队列
 * 优先级越高，位置越靠前
 */
function PriorityQueue(){
  var items = [];

  var Node = function(element, priority){
    this.element = element;
    this.priority = priority;
  }

  this.enqueue = function(element, priority = 0){
    var node = new Node(element, priority);
    if (items.length === 0) {
      items.push(node);
    } else {
      var added = false;
      for (var i = 0; i < items.length; i++) {
        if(node.priority > items[i].priority){
          items.splice(i, 0, node);
          added = true;
          break;
        }
      }
      if(!added){
        items.push(node);
      }
    }
  }

  this.dequeue = function(){
    return items.shift();
  }

  this.peek = function(){
    return items[0];
  }

  this.isEmpty = function(){
    return items.length === 0;
  }

  this.size = function(){
    return items.length;
  }

  this.clear = function(){
    return items = [];
  }
}